﻿/*
   Copyright 2012 Dmitry Bratus

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Flower.Services.Data;
using Assembly = Flower.Services.Data.Assembly;

namespace Flower.MemoryDirectory
{
    internal class DirectoryEntryContainer
    {
        private DirectoryEntry _entry;
        private DirectoryEntryTypes _entryType;

        public DirectoryEntryTypes EntryType
        {
            get { return _entryType; }
        }

        public DirectoryEntry Entry
        {
            get { return _entry; }
            set
            {
                _entry = value;

                #region Entry type determination

                if (value is Assembly)
                {
                    _entryType = DirectoryEntryTypes.Assembly;
                } 
                else if (value is Folder)
                {
                    _entryType = DirectoryEntryTypes.Folder;
                }
                else if (value is Link)
                {
                    _entryType = DirectoryEntryTypes.Link;
                }
                else if (value is Message)
                {
                    _entryType = DirectoryEntryTypes.Message;
                }
                else if (value is Process)
                {
                    _entryType = DirectoryEntryTypes.Process;
                }
                else if (value is Processor)
                {
                    _entryType = DirectoryEntryTypes.Processor;
                }
                else if (value is Role)
                {
                    _entryType = DirectoryEntryTypes.Role;
                }
                else if (value is ServiceContainer)
                {
                    _entryType = DirectoryEntryTypes.ServiceContainer;
                }
                else if (value is Set)
                {
                    _entryType = DirectoryEntryTypes.Set;
                }
                else if (value is State)
                {
                    _entryType = DirectoryEntryTypes.State;
                }
                else if (value is Workflow)
                {
                    _entryType = DirectoryEntryTypes.Workflow;
                }

                #endregion
            }
        }

        public DirectoryEntryContainer Parent { get; set; }
        public List<DirectoryEntryContainer> Children { get; private set; } 

        public DirectoryEntryContainer()
        {
            Children = new List<DirectoryEntryContainer>();
        }

        public void RemoveFromParent(Action<string> idRemoved)
        {
            Parent.Children.Remove(this);
            idRemoved(Entry.Id);

            DirectoryEntryContainer set = GetAncestor(DirectoryEntryTypes.Set, 0);
            if (set != null)
            {
                Parent.CutEmptySubsets(idRemoved);
            }
        }

        public int ChildrenCount(DirectoryEntryTypes entryTypes)
        {
            return Children.Count(ent => entryTypes.HasFlag(ent.EntryType));
        }

        private void CutEmptySubsets(Action<string> idRemoved)
        {
            if (EntryType == DirectoryEntryTypes.Folder && Children.Count == 0)
            {
                Parent.Children.Remove(this);
                idRemoved(Entry.Id);
                Parent.CutEmptySubsets(idRemoved);
            }
        }

        public DirectoryEntryContainer GetAncestor(DirectoryEntryTypes ancestorEntryTypes, int n)
        {
            DirectoryEntryContainer ancEnt = this;

            while (true)
            {
                if (ancestorEntryTypes.HasFlag(ancEnt.EntryType))
                {
                    if (n > 0)
                    {
                        --n;
                    }
                    else
                    {
                        return ancEnt;
                    }
                }

                ancEnt = ancEnt.Parent;

                if (ancEnt == null)
                {
                    return null;
                }
            }
        }

        public string GetPath()
        {
            DirectoryEntryContainer ent = this;
            var path = new List<string>();

            do
            {
                path.Add(ent.Entry.Name);
                ent = ent.Parent;
            }
            while (ent.Parent != null);

            path.Reverse();

            return "/" + string.Join("/", path);
        }

        public DirectoryEntryContainer GetDeepestExistingRecursively(string[] path, int i, out int depth)
        {
            depth = i;

            if (i == path.Length)
            {
                return this;
            }

            string childName = path[i];

            foreach (DirectoryEntryContainer child in Children)
            {
                if (child.Entry.Name == childName)
                {
                    return child.GetDeepestExistingRecursively(path, i + 1, out depth);
                }
            }

            return this;
        }

        public DirectoryEntryContainer GetRecursively(string[] path, int i)
        {
            if (i == path.Length)
            {
                return this;
            }
            else
            {
                string childName = path[i];

                foreach (DirectoryEntryContainer child in Children)
                {
                    if (child.Entry.Name == childName)
                    {
                        return child.GetRecursively(path, i + 1);
                    }
                }

                return null;
            }
        }
    }
}
